<?php

namespace app\admin\controller;

use app\admin\model\ShopLicense as ShopLicenseModel;
use app\shop\model\ShopInfo;
use think\Controller;
use think\Db;
use think\facade\Request;
use think\facade\Validate;

class Shop extends Controller
{

    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'
    ];


    //检验一下创建商家所需的数据是否合法
    private function checksCreateShopData($data){

        $rule = [
              'shopname|店铺名'=>'require|max:15',
              'mobile|联系电话'=>'require|mobile',
              'password|密码'=>'require|min:6|max:20',
              "email|邮箱"=>'require|email|min:3|max:20',
              "dada_shop_no|达达配送商户号"=>'require|min:1|max:10',
              "dada_city_code|达达城市编号"=>'require|min:1|max:10',
        ];

        //验证
        $validate   = Validate::make($rule);
        $result = $validate->check($data);

        //数据验证失败
        if(!$result) {
            $this->result('',400,$validate->getError());
            die();
        }

        //判断手机号是否已经添加过了

            $shop=\app\admin\model\Shop::where(['mobile'=>$data['mobile']])->find();
        if($shop!=null){
            $this->result('',400,'该手机号已经绑定了账号了');
            die();
        }

    }



    //添加商家
    public function create()
    {
        $data= Request::only(['shopname','mobile','password','manager_id','email','dada_shop_no','dada_city_code']);

        //数据校验
        $this->checksCreateShopData($data);

        try {
            $shop = \app\shop\model\Shop::create([
                'shopname' => $data['shopname'],
                'mobile' => $data['mobile'],
                "email"=>$data['email'],
                "manager_id"=>$data['manager_id'],
                'password' => md5($data['password'])
            ]);

            //创建一个最基本的商家信息
           $res= ShopInfo::initShopInfo($shop->id, $data['shopname'], $data['mobile']);

           //修改初始化的信息
            $res2=ShopInfo::where(['shop_id'=>$shop->id])->update([
                "dada_shop_no"=>$data['dada_shop_no'],
                "dada_city_code"=>$data['dada_city_code']
            ]);

            $this->result($data,200,'创建成功');
        } catch (\Throwable $e) {
            Db::table('shop')->where(['mobile'=>$data['mobile']])->delete();
            $this->result('',400,$e->getMessage());
        }

    }



    public function index()
    {

        $page=Request::param('page',1);
        $pageSize=Request::param('pageSize',20);
        $key=Request::param('key',null);
        $where=[];

        if($key){
            array_push($where,[
                'shop_info.shopname','like','%'.$key.'%'
            ]);
        }

        //商家数量
        $count=Db::table('shop')->count('id');

        $page_num=ceil($count/$pageSize);

        $list=  \app\admin\model\Shop:: where(['status'=>10])

            ->leftJoin('shop_info','shop.id=shop_info.shop_id')
            ->where($where)
            ->field(['shop.id','shop.mobile','shop.manager_id','shop.money','shop.add_time',
                'shop_info.*'])
            ->page($page,$pageSize)
            ->select();

        $data=[
            'list'=>$list,
            'page_num'=>$page_num,
            'count'=>$count,
            'page_current'=>$page
        ];

        return toResful(200,$data,'');

    }







}
